/*--------------------------------------------------------------------------------
	DESCRIPTION: Cleaning Afrobarometer

--------------------------------------------------------------------------------*/

*Afrobarometer 1

clear
use "$data_dir/raw/Afro Barometer/merged_r1_data.dta"

rename country country_afro
decode country_afro, gen(country)

replace country = "United Republic of Tanzania" if country=="Tanzania"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Afrobarometer"
gen wave = 1

gen weight = combinwt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = .

gen trust_justice = 1 if trscts==3|trscts==4
replace trust_justice = 0 if trscts==1|trscts==2

gen trust_police = 1 if trspol==3|trspol==4
replace trust_police = 0 if trspol==1|trspol==2

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = .

gen trust_parliament = .

gen trust_others = 1 if sctrust==1
replace trust_others = 0 if sctrust==2

gen trust_military = 1 if trsarm==3|trsarm==4
replace trust_military = 0 if trsarm==1|trsarm==2

pca trust_justice trust_police trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

//this refers to "fieldwork" given exact date of interview is not given for this round
gen survey_year = .
replace survey_year = 1999 if ccode=="BWA"
replace survey_year = 1999 if ccode=="GHA"
replace survey_year = 2000 if ccode=="LSO"
replace survey_year = 1999 if ccode=="MWI"
replace survey_year = 2001 if ccode=="MLI"
replace survey_year = 1999 if ccode=="NAM"
replace survey_year = 2000 if ccode=="NGA"
replace survey_year = 2000 if ccode=="ZAF"
replace survey_year = 2001 if ccode=="TZA"
replace survey_year = 2000 if ccode=="UGA"
replace survey_year = 1999 if ccode=="ZMB"
replace survey_year = 1999 if ccode=="ZWE"

replace age = . if age==0|age==101|age==999

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if gender==1
replace sex = 2 if gender==2
replace sex = -99 if gender==.|gender==99

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*		  -99   Missing 

gen religion = -99



*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if educ==1
replace edu = 2 if educ==2
replace edu = 3 if educ==3
replace edu = 4 if educ==0
replace edu = -99 if educ==0|educ==10|educ==98|educ==99

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(occup, 17, 41, 42)
replace occupation = 2 if inlist(occup, 3, 9, 12, 18, 23, 28)
replace occupation = 3 if inlist(occup, 8, 10, 11, 21, 31, 44, 46)
replace occupation = 4 if inlist(occup, 4, 45)
replace occupation = 5 if inlist(occup, 20, 30)
replace occupation = 6 if inlist(occup, 52)
replace occupation = 7 if inlist(occup, 5, 33, 47, 48)
replace occupation = 8 if inlist(occup, 7, 19, 49, 50, 51)
replace occupation = 9 if inlist(occup, 1, 2, 6, 22, 24, 32, 55, 53, 54)
replace occupation = 0 if inlist(occup, 29, 56)
replace occupation = -99 if inlist(occup, 0, 13, 14, 15, 26, 27, 59, 60, 61, 95, 98, 99)


*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if unemp==0
replace employment_status = 2 if unemp==1
replace employment_status = -99 if unemp==.|unemp==98|unemp==99

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = -99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if urbrur==1
replace urban = 2 if urbrur==2
replace urban = -99 if urbrur==9|urbrur==99|urbrur==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight  

sort ccode wave survey_year

save "$data_dir/clean/afro1_clean.dta", replace

***************************************************************************************************************

*Afrobarometer 2

clear
use "$data_dir/raw/Afro Barometer/merged_r2_data.dta"

rename country country_afro
decode country_afro, gen(country)

replace country = "United Republic of Tanzania" if country=="Tanzania"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Afrobarometer"
gen wave = 2

gen weight = combinwt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = .

gen trust_justice = 1 if q43j==3|q43j==2
replace trust_justice = 0 if q43j==1|q43j==0

gen trust_police = 1 if q43i==3|q43i==2
replace trust_police = 0 if q43i==1|q43i==0

gen trust_civil_service = .

gen trust_local_govt = 1 if q43e==3|q43e==2
replace trust_local_govt = 0 if q43e==1|q43e==0

gen trust_parties = 1 if (q43f==3|q43f==2) & (q43g==3|q43g==2)
replace trust_parties = 0 if (q43f==1|q43f==0) & (q43g==1|q43g==0)

gen trust_parliament = 1 if q43b==3|q43b==2
replace trust_parliament = 0 if q43b==1|q43b==0

gen trust_others = .

gen trust_military = 1 if q43h==2|q43h==3
replace trust_military = 0 if q43h==0|q43h==1

pca trust_justice trust_police trust_local_govt trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = year(dateintr) if dateintr!=.
replace survey_year = 2002 if ccode=="SEN" & survey_year==.

clonevar age = q80
replace age = . if age==-1|age==998|age==999

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if q96==1
replace sex = 2 if q96==2
replace sex = -99 if q96==.|q96==99

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if q85==0|q85==8|q85==9
replace religion = 5 if q85==1|q85==380|q85==381|q85==382|q85==383|q85==385|q85==386
replace religion = 4 if q85==2|q85==3|q85==4|q85==5|q85==10|q85==11|q85==100|q85==101|q85==102|q85==103
replace religion = 6 if q85==6|q85==7|q85==12|q85==50|q85==51|q85==52|q85==387|q85==388|q85==995
replace religion = 7 if q85==7
replace religion = -99 if q85==-1|q85==998|q85==999

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if q84==2|q84==3
replace edu = 2 if q84==4|q84==5
replace edu = 3 if q84==6|q84==7|q84==8|q84==9
replace edu = 4 if q84==0|q84==1
replace edu = -99 if q84==-1|q84==98|q84==99

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(q88, 16)
replace occupation = 2 if inlist(q88, 7, 8, 15, 261, 380, 381)
replace occupation = 3 if inlist(q88, 9, 382)
replace occupation = 4 if inlist(q88, 10, 160)
replace occupation = 5 if inlist(q88, 22)
replace occupation = 6 if inlist(q88, 162)
replace occupation = 7 if inlist(q88, 23, 24, 180)
replace occupation = 8 if inlist(q88, 12, 25, 26, 181)
replace occupation = 9 if inlist(q88, 1, 2, 3, 4, 5, 6, 14, 27, 60, 262, 263)
replace occupation = 0 if inlist(q88, 17, 50, 260)
replace occupation = -99 if inlist(q88, -1, 0, 18, 19, 161, 990, 991, 992, 993, 995, 998, 999)

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if q89==2|q89==3|q89==4|q89==5
replace employment_status = 2 if q89==0|q89==1
replace employment_status = -99 if q89==.|q89==9|q89==98|q89==-1

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

clonevar income = q90
replace income = -99 if income==-1|income==0|income==98|income==99|income==.

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = -99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if urbrur==1
replace urban = 2 if urbrur==2
replace urban = -99 if urbrur==9|urbrur==99|urbrur==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight  

sort ccode wave survey_year

save "$data_dir/clean/afro2_clean.dta", replace

***************************************************************************************************************

*Afrobarometer 3

clear
use "$data_dir/raw/Afro Barometer/merged_r3_data.dta"

rename country country_afro
decode country_afro, gen(country)

replace country = "United Republic of Tanzania" if country=="Tanzania"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Afrobarometer"
gen wave = 3

gen weight = combinwt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = .

gen trust_justice = 1 if q55i==3|q55i==2
replace trust_justice = 0 if q55i==1|q55i==0

gen trust_police = 1 if q55h==3|q55h==2
replace trust_police = 0 if q55h==1|q55h==0

gen trust_civil_service = .

gen trust_local_govt = 1 if q55d==3|q55d==2
replace trust_local_govt = 0 if q55d==1|q55d==0

gen trust_parties = 1 if (q55e==3|q55e==2) & (q55f==3|q55f==2)
replace trust_parties = 0 if (q55e==1|q55e==0) & (q55f==1|q55f==0)

gen trust_parliament = 1 if q55b==3|q55b==2
replace trust_parliament = 0 if q55b==1|q55b==0

gen trust_others = 1 if q83==1
replace trust_others = 0 if q83==0

gen trust_military = 1 if q55g==2|q55g==3
replace trust_military = 0 if q55g==0|q55g==1

pca trust_justice trust_police trust_local_govt trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = year(dateintr) if dateintr!=.

clonevar age = q1
replace age = . if age==-1|age==998|age==999

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if q101==1
replace sex = 2 if q101==2
replace sex = -99 if q101==.|q101==99

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if q91==0|q91==8|q91==9
replace religion = 5 if q91==11|q91==12|q91==15|q91==360|q91==361|q91==362|q91==363
replace religion = 4 if q91==2|q91==3|q91==4|q91==5|q91==10|q91==13|q91==14
replace religion = 6 if q91==6|q91==7|q91==995
replace religion = 7 if q91==7
replace religion = -99 if q91==-1|q91==998|q91==999

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if q90==2|q90==3
replace edu = 2 if q90==4|q90==5
replace edu = 3 if q90==6|q90==7|q90==8|q90==9
replace edu = 1 if q90==0|q90==1
replace edu = -99 if q90==-1|q90==99

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(q95, 15, 16, 17, 21)
replace occupation = 2 if inlist(q95, 18, 19, 20)
replace occupation = 5 if inlist(q95, 22)
replace occupation = 6 if inlist(q95, 3)
replace occupation = 7 if inlist(q95, 10, 11, 12)
replace occupation = 8 if inlist(q95, 7, 13, 14)
replace occupation = 9 if inlist(q95, 1, 2, 4, 5, 6, 8, 25)
replace occupation = 0 if inlist(q95, 9)
replace occupation = -99 if inlist(q95, -1, 0, 23, 24, 990, 991, 992, 995, 998, 999)

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if q94==2|q94==3|q94==4|q94==5
replace employment_status = 2 if q94==0|q94==1
replace employment_status = -99 if q94==.|q94==9|q94==998|q94==-1

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = -99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if urbrur==1
replace urban = 2 if urbrur==2
replace urban = -99 if urbrur==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight  

sort ccode wave survey_year

save "$data_dir/clean/afro3_clean.dta", replace

***************************************************************************************************************

*Afrobarometer 4

clear
use "$data_dir/raw/Afro Barometer/merged_r4_data.dta"

rename country country_afro
decode country_afro, gen(country)

replace country = "United Republic of Tanzania" if country=="Tanzania"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Afrobarometer"
gen wave = 4

gen weight = Combinwt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = .

gen trust_justice = 1 if Q49H==3|Q49H==2
replace trust_justice = 0 if Q49H==1|Q49H==0

gen trust_police = 1 if Q49G==3|Q49G==2
replace trust_police = 0 if Q49G==1|Q49G==0

gen trust_civil_service = .

gen trust_local_govt = 1 if Q49D==3|Q49D==2
replace trust_local_govt = 0 if Q49D==1|Q49D==0

gen trust_parties = 1 if (Q49E==3|Q49E==2) & (Q49F==3|Q49F==2)
replace trust_parties = 0 if (Q49E==1|Q49E==0) & (Q49F==1|Q49F==0)

gen trust_parliament = 1 if Q49B==3|Q49B==2
replace trust_parliament = 0 if Q49B==1|Q49B==0

gen trust_others = 1 if Q84C==2|Q84C==3 //N.B. only available for Ghana
replace trust_others = 0 if Q84C==0|Q84C==1

gen trust_military = .

pca trust_justice trust_police trust_local_govt trust_parliament, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = year(dateintr) if dateintr!=.

clonevar age = Q1
replace age = . if age==-1|age==998|age==999

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if Q101==1
replace sex = 2 if Q101==2
replace sex = -99 if Q101==.|Q101==99

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*		  -99   Missing 

gen religion = .
replace religion = 1 if Q90==0|Q90==28|Q90==29
replace religion = 5 if Q90==18|Q90==19|Q90==20|Q90==21|Q90==22|Q90==23|Q90==24|Q90==502|Q90==620
replace religion = 4 if Q90==1|Q90==2|Q90==3|Q90==4|Q90==5|Q90==6|Q90==7|Q90==8|Q90==9|Q90==10|Q90==11|Q90==12|Q90==13|Q90==14|Q90==15|Q90==16|Q90==30|Q90==140|Q90==142|Q90==143|Q90==420|Q90==463|Q90==464|Q90==580|Q90==700|Q90==701|Q90==861|Q90==862|Q90==860
replace religion = 6 if Q90==17|Q90==25|Q90==26|Q90==27|Q90==141|Q90==461|Q90==462|Q90==465|Q90==500|Q90==501|Q90==504|Q90==660|Q90==995
replace religion = 7 if Q90==26
replace religion = -99 if Q90==-1|Q90==998|Q90==999

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if Q89==2|Q89==3
replace edu = 2 if Q89==4|Q89==5
replace edu = 3 if Q89==6|Q89==7|Q89==8|Q89==9
replace edu = 4 if Q89==0|Q89==1
replace edu = -99 if Q89==-1|Q89==99

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = -99

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if Q94==2|Q94==3|Q94==4|Q94==5
replace employment_status = 2 if Q94==0|Q94==1
replace employment_status = -99 if Q94==.|Q94==9|Q94==-1


*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = -99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if urbrur==1
replace urban = 2 if urbrur==2
replace urban = -99 if urbrur==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight  

sort ccode wave survey_year

save "$data_dir/clean/afro4_clean.dta", replace

***************************************************************************************************************

*Afrobarometer 5

clear
use "$data_dir/raw/Afro Barometer/merged_r5_data.dta"

rename country country_afro
decode country_afro, gen(country)

replace country = "United Republic of Tanzania" if country=="Tanzania"
replace country = "Côte d'Ivoire" if country_afro==27

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Afrobarometer"
gen wave = 5

gen weight = combinwt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = .

gen trust_justice = 1 if Q59J==3|Q59J==2
replace trust_justice = 0 if Q59J==1|Q59J==0

gen trust_police = 1 if Q59H==3|Q59H==2
replace trust_police = 0 if Q59H==1|Q59H==0

gen trust_civil_service = .

gen trust_local_govt = 1 if Q59E==3|Q59E==2
replace trust_local_govt = 0 if Q59E==1|Q59E==0

gen trust_parties = 1 if (Q59F==3|Q59F==2) & (Q59G==3|Q59G==2)
replace trust_parties = 0 if (Q59F==1|Q59F==0) & (Q59G==1|Q59G==0)

gen trust_parliament = 1 if Q59B==3|Q59B==2
replace trust_parliament = 0 if Q59B==1|Q59B==0

gen trust_others = 1 if Q87==1
replace trust_others = 0 if Q87==0

gen trust_military = 1 if Q59I==2|Q59I==3
replace trust_military = 0 if Q59I==0|Q59I==1

pca trust_justice trust_police trust_local_govt trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = year(dateintr) if dateintr!=.

clonevar age = Q1
replace age = . if age==-1|age==998|age==999

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if Q101==1
replace sex = 2 if Q101==2
replace sex = -99 if Q101==.|Q101==99

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(Q98A, 0, 28, 29)
replace religion = 2 if inlist(Q98A, 900)
replace religion = 4 if inlist(Q98A, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 30, 31, 32, 33, 34, 35, 36, 37, 144, 145, 220, 420, 421, 422, 461, 462, 463, 464, 465, 466, 500, 501, 540, 542, 543, 701, 702, 780, 821, 822, 824, 860, 861, 862, 864, 865, 1140, 1260, 1261, 1262)
replace religion = 5 if inlist(Q98A, 18, 19, 20, 21, 22, 23, 24, 620, 930, 931, 1420)
replace religion = 6 if inlist(Q98A, 17, 25, 26, 27, 902, 903)
replace religion = 7 if Q98A==26
replace religion = -99 if inlist(Q98A, -1, 9998, 9999)
replace religion = 6 if religion==. & Q98A!=.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if Q97==2|Q97==3
replace edu = 2 if Q97==4|Q97==5
replace edu = 3 if Q97==6|Q97==7|Q97==8|Q97==9
replace edu = 4 if Q97==0|Q97==1
replace edu = -99 if Q97==-1|Q97==99

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(Q96_ARB, 15, 16, 17, 21)
replace occupation = 2 if inlist(Q96_ARB, 18, 19, 20)
replace occupation = 4 if inlist(Q96_ARB, 12)
replace occupation = 5 if inlist(Q96_ARB, 22)
replace occupation = 6 if inlist(Q96_ARB, 3)
replace occupation = 7 if inlist(Q96_ARB, 10, 11)
replace occupation = 8 if inlist(Q96_ARB, 7, 13, 14)
replace occupation = 9 if inlist(Q96_ARB, 1, 2, 4, 5, 6, 8)
replace occupation = 0 if inlist(Q96_ARB, 9)
replace occupation = -99 if inlist(Q96_ARB, -1, 0, 23, 24, 995, 998, 999, 1542)
replace occupation = -99 if Q96_ARB==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if Q96==2|Q96==3
replace employment_status = 2 if Q96==0|Q96==1
replace employment_status = -99 if Q96==.|Q96==9|Q96==-1

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = -99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if urbrur==1
replace urban = 2 if urbrur==2
replace urban = -99 if urbrur==.|urbrur==3 //cannot classify "semi-urban"

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight  

sort ccode wave survey_year

save "$data_dir/clean/afro5_clean.dta", replace

***************************************************************************************************************

*Afrobarometer 6

clear
use "$data_dir/raw/Afro Barometer/merged_r6_data.dta"

rename COUNTRY country_afro
decode country_afro, gen(country)

replace country = "United Republic of Tanzania" if country=="Tanzania"
replace country = "Côte d'Ivoire" if country=="Cote d'Ivoire"
replace country = "Sao Tome and Principe" if country=="São Tomé and Príncipe"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Afrobarometer"
gen wave = 6

gen weight = Combinwt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = .

gen trust_justice = 1 if Q52J==3|Q52J==2
replace trust_justice = 0 if Q52J==1|Q52J==0

gen trust_police = 1 if Q52H==3|Q52H==2
replace trust_police = 0 if Q52H==1|Q52H==0

gen trust_civil_service = .

gen trust_local_govt = 1 if Q52E==3|Q52E==2
replace trust_local_govt = 0 if Q52E==1|Q52E==0

gen trust_parties = 1 if (Q52F==3|Q52F==2) & (Q52G==3|Q52G==2)
replace trust_parties = 0 if (Q52F==1|Q52F==0) & (Q52G==1|Q52G==0)

gen trust_parliament = 1 if Q52B==3|Q52B==2
replace trust_parliament = 0 if Q52B==1|Q52B==0

gen trust_others = .

gen trust_military = 1 if Q52I==2|Q52I==3
replace trust_military = 0 if Q52I==0|Q52I==1

pca trust_justice trust_police trust_local_govt trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

format DATEINTR %tc

gen survey_year = yofd(dofc(DATEINTR))

clonevar age = Q1
replace age = . if age==-1|age==998|age==999

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if Q101==1
replace sex = 2 if Q101==2
replace sex = -99 if Q101==.|Q101==99

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(Q98A, 0, 28, 29)
replace religion = 2 if inlist(Q98A, 900)
replace religion = 3 if inlist(Q98A, 34)
replace religion = 4 if inlist(Q98A, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 30, 31, 32, 33, 35, 36, 37, 144, 145, 220, 421, 422, 461, 462, 463, 821, 822, 860, 1260)
replace religion = 5 if inlist(Q98A, 18, 19, 20, 21, 22, 23, 24, 540, 620, 930, 931, 1420)
replace religion = 6 if inlist(Q98A, 25, 26, 27, 144, 500, 501, 543, 701, 702, 821, 862, 900, 901, 902, 903, 9995)
replace religion = 7 if Q98A==26
replace religion = -99 if inlist(Q98A, -1, 9998, 9999)

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if Q97==2|Q97==3
replace edu = 2 if Q97==4|Q97==5
replace edu = 3 if Q97==6|Q97==7|Q97==8|Q97==9
replace edu = 4 if Q97==0|Q97==1
replace edu = -99 if Q97==-1|Q97==98|Q97==99

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(Q96A, 9)
replace occupation = 2 if inlist(Q96A, 12)
replace occupation = 3 if inlist(Q96A, 11)
replace occupation = 4 if inlist(Q96A, 8)
replace occupation = 5 if inlist(Q96A, 5)
replace occupation = 6 if inlist(Q96A, 3)
replace occupation = 7 if inlist(Q96A, 7)
replace occupation = 8 if inlist(Q96A, 6)
replace occupation = 9 if inlist(Q96A, 4)
replace occupation = 0 if inlist(Q96A, 10)
replace occupation = -99 if inlist(Q96A, -1, 0, 1, 2, 95, 98, 99)

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if Q95==2|Q95==3
replace employment_status = 2 if Q95==0|Q95==1
replace employment_status = -99 if Q95==.|Q95==9|Q95==-1


*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = -99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if URBRUR==1
replace urban = 2 if URBRUR==2
replace urban = -99 if URBRUR==.|URBRUR==3 //cannot classify "semi-urban"

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight  

sort ccode wave survey_year

save "$data_dir/clean/afro6_clean.dta", replace

***************************************************************************************************************

*Afrobarometer 7

clear
use "$data_dir/raw/Afro Barometer/merged_r7_data.dta"

rename COUNTRY country_afro
decode country_afro, gen(country)

replace country = "United Republic of Tanzania" if country=="Tanzania"
replace country = "Côte d'Ivoire" if country=="Cote d'Ivoire"
replace country = "Sao Tome and Principe" if country=="São Tomé and Príncipe"
replace country = "Swaziland" if country=="eSwatini"
replace country = "Cape Verde" if country=="Cabo Verde"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Afrobarometer"
gen wave = 7

gen weight = Combinwt

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = .

gen trust_justice = 1 if Q43I==3|Q43I==2
replace trust_justice = 0 if Q43I==1|Q43I==0

gen trust_police = 1 if Q43G==3|Q43G==2
replace trust_police = 0 if Q43G==1|Q43G==0

gen trust_civil_service = .

gen trust_local_govt = 1 if Q43D==3|Q43D==2
replace trust_local_govt = 0 if Q43D==1|Q43D==0

gen trust_parties = 1 if (Q43E==3|Q43E==2) & (Q43F==3|Q43F==2)
replace trust_parties = 0 if (Q43E==1|Q43E==0) & (Q43F==1|Q43F==0)

gen trust_parliament = 1 if Q43B==3|Q43B==2
replace trust_parliament = 0 if Q43B==1|Q43B==0

gen trust_others = .

gen trust_military = 1 if Q43H==2|Q43H==3
replace trust_military = 0 if Q43H==0|Q43H==1

pca trust_justice trust_police trust_local_govt trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = yofd(dofc(DATEINTR))

clonevar age = Q1
replace age = . if age==-1|age==998|age==999

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if Q101==1
replace sex = 2 if Q101==2
replace sex = -99 if Q101==.|Q101==99|Q101==-1

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(Q98, 0, 28, 29)
replace religion = 3 if inlist(Q98, 34)
replace religion = 4 if inlist(Q98, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 30, 31, 32, 33, 100, 220, 260, 460, 541, 700, 742, 743, 744, 745, 821, 822, 823, 824, 1100, 1220, 1620, 1621, 1622, 1660, 1661, 1662, 1700, 1701, 1702)
replace religion = 5 if inlist(Q98, 18, 19, 20, 21, 22, 23, 24, 620, 660, 931, 1100)
replace religion = 6 if inlist(Q98, 25, 26, 27, 9995)
replace religion = 7 if Q98==26
replace religion = -99 if inlist(Q98, -1, 9998, 9999)

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if Q97==2|Q97==3
replace edu = 2 if Q97==4|Q97==5
replace edu = 3 if Q97==6|Q97==7|Q97==8|Q97==9
replace edu = 4 if Q97==0|Q97==1
replace edu = -99 if Q97==-1|Q97==98|Q97==99

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(Q95A, 9)
replace occupation = 2 if inlist(Q95A, 12)
replace occupation = 3 if inlist(Q95A, 11)
replace occupation = 4 if inlist(Q95A, 8)
replace occupation = 5 if inlist(Q95A, 5)
replace occupation = 6 if inlist(Q95A, 3)
replace occupation = 7 if inlist(Q95A, 7)
replace occupation = 8 if inlist(Q95A, 6)
replace occupation = 9 if inlist(Q95A, 4)
replace occupation = 0 if inlist(Q95A, 10)
replace occupation = -99 if inlist(Q95A, -1, 0, 1, 2, 95, 98, 99)

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if Q94==2|Q94==3
replace employment_status = 2 if Q94==0|Q94==1
replace employment_status = -99 if Q94==.|Q94==9|Q94==8|Q94==-1

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = -99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if URBRUR==1
replace urban = 2 if URBRUR==2
replace urban = -99 if URBRUR==.|URBRUR==3 //cannot classify "semi-urban"

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight  

sort ccode wave survey_year

save "$data_dir/clean/afro7_clean.dta", replace

***************************************************************************************************************

*Afrobarometer 8

clear
use "$data_dir/raw/Afro Barometer/merged_r8_data.dta"

rename COUNTRY country_afro
decode country_afro, gen(country)

replace country = "United Republic of Tanzania" if country=="Tanzania"
replace country = "Sao Tome and Principe" if country=="São Tomé and Príncipe"
replace country = "Swaziland" if country=="Eswatini"
replace country = "Cape Verde" if country=="Cabo Verde"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "Afrobarometer"
gen wave = 8

gen weight = Combinwt_old_ea

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = .

gen trust_justice = 1 if Q41I==3|Q41I==2
replace trust_justice = 0 if Q41I==1|Q41I==0

gen trust_police = 1 if Q41G==3|Q41G==2
replace trust_police = 0 if Q41G==1|Q41G==0

gen trust_civil_service = .

gen trust_local_govt = 1 if Q41D==3|Q41D==2
replace trust_local_govt = 0 if Q41D==1|Q41D==0

gen trust_parties = 1 if (Q41E==3|Q41E==2) & (Q41F==3|Q41F==2)
replace trust_parties = 0 if (Q41E==1|Q41E==0) & (Q41F==1|Q41F==0)

gen trust_parliament = 1 if Q41B==3|Q41B==2
replace trust_parliament = 0 if Q41B==1|Q41B==0

gen trust_others = 1 if Q83==1
replace trust_others = 0 if Q83==0

gen trust_military = 1 if Q41H==2|Q41H==3
replace trust_military = 0 if Q41H==0|Q41H==1

pca trust_justice trust_police trust_local_govt trust_parliament trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = yofd(dofc(DATEINTR))

clonevar age = Q1
replace age = . if age==-1|age==998|age==999

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if Q101==1
replace sex = 2 if Q101==2
replace sex = -99 if Q101==.|Q101==99|Q101==-1

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(Q98A, 0, 28, 29) 
replace religion = 3 if inlist(Q98A, 34) 
replace religion = 4 if inlist(Q98A, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 30, 31, 32, 33, 100, 260, 261, 300, 340, 460, 462, 463, 742, 820, 821, 1260, 1750, 1751, 1752, 1553, 1754, 1755, 1756, 1757, 1758) 
replace religion = 5 if inlist(Q98A, 18, 19, 20, 21, 22, 23, 24) 
replace religion = 6 if inlist(Q98A, 25, 26, 27, 900, 1340, 9995) 
replace religion = 7 if Q98A==26
replace religion = -99 if inlist(Q98A, -1, 9998, 9999) 

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if Q97==2|Q97==3
replace edu = 2 if Q97==4|Q97==5
replace edu = 3 if Q97==6|Q97==7|Q97==8|Q97==9
replace edu = 4 if Q97==0|Q97==1
replace edu = -99 if Q97==-1|Q97==98|Q97==99

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if inlist(Q95C, 700, 9)
replace occupation = 2 if inlist(Q95C, 11, 12)
replace occupation = 4 if inlist(Q95C, 8)
replace occupation = 5 if inlist(Q95C, 5, 10)
replace occupation = 6 if inlist(Q95C, 3)
replace occupation = 7 if inlist(Q95C, 7)
replace occupation = 9 if inlist(Q95C, 4, 6)
replace occupation = -99 if inlist(Q95C, -1, 0, 1, 2, 220, 1500, 1580, 1700, 9995, 9998, 9999)


*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if Q95A==2|Q95A==3
replace employment_status = 2 if Q95A==0|Q95A==1
replace employment_status = -99 if Q95A==.|Q95A==9|Q95A==8|Q95A==-1

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = -99

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if URBRUR==1
replace urban = 2 if URBRUR==2
replace urban = -99 if URBRUR==.|URBRUR==3 //cannot classify "semi-urban"

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military  survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight  

sort ccode wave survey_year

save "$data_dir/clean/afro8_clean.dta", replace
